package com.lz.gmall.product.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lz.gmall.common.result.Result;
import com.lz.gmall.common.result.ResultCodeEnum;
import com.lz.gmall.product.entity.BaseTrademark;
import com.lz.gmall.product.service.BaseTrademarkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
@Api(tags = "后台品牌列表操作")
public class BaseTrademarkController {

    @Autowired
    private BaseTrademarkService baseTrademarkService;

    /**
     * @param pageNum 页码
     * @param limit 每天条数
     * @return 分页所有品牌
     */
    @GetMapping("/baseTrademark/{page}/{limit}")
    @ApiOperation(value = "分页获取所有品牌")
    public Result<Page<BaseTrademark>> getBaseTrademarkByPage(@PathVariable(value = "page") Integer pageNum,@PathVariable(value = "limit") Integer limit){

        Page<BaseTrademark> page = baseTrademarkService.getBaseTrademarkByPage(pageNum,limit);
        return Result.build(page, ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加品牌
     * @param baseTrademark 品牌信息
     * @return 统一返回对象
     */
    @PostMapping("/baseTrademark/save")
    @ApiOperation("添加品牌")
    public Result saveBaseTrademark(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.saveBaseTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 修改品牌信息，先根据获取品牌信息回显
     * @param baseTrademark 修改的品牌信息
     * @return 统一回显数据
     */
    @PutMapping("/baseTrademark/update")
    @ApiOperation("修改品牌信息")
    public Result updateBaseTrademark(@RequestBody BaseTrademark baseTrademark){
        baseTrademarkService.updateBaseTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 修改品牌信息进行数据回显
     * @param id 品牌id
     * @return 品牌信息
     */
    @GetMapping("/baseTrademark/get/{id}")
    @ApiOperation("根据id获取品牌")
    public Result<BaseTrademark> getBaseTrademarkById(@PathVariable(value = "id") Long id){
        BaseTrademark baseTrademark = baseTrademarkService.getById(id);
        return Result.build(baseTrademark,ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除品牌，要先判断品牌是否关联spu，sku
     * 关联不允许删，无关联可以删
     * @param id 品牌id
     * @return 统一返回数据
     */
    @DeleteMapping("/baseTrademark/remove/{id}")
    @ApiOperation("根据id删除品牌")
    public Result deleteBaseTrademarkById(@PathVariable(value = "id") Long id){
        baseTrademarkService.deleteBaseTrademarkById(id);
        return Result.ok();
    }

    /**
     * 在添加spu产品，需要回显所有品牌参数
     * @return 所有品牌集合
     */
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result<List<BaseTrademark>> getTrademarkList(){
        List<BaseTrademark> list = baseTrademarkService.list();
        return Result.build(list,ResultCodeEnum.SUCCESS);
    }
}
